package com.karl.mytask_2.tools;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.karl.mytask_2.R;
import com.karl.mytask_2.activity.PublishActivity;
import com.karl.mytask_2.bean.Task;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Karl on 2016/5/20.
 *
 */
public abstract class MyBaseFragment<T> extends Fragment implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    public RecyclerView mRecyclerView;
    public MyAdapter mAdapter;
    public List<T> mList = new ArrayList<>();
    public TextView mAppName,createTask;
    public View mView;
    //刷新组件
    View mEmptyView;
    View mLoadingView;
    private TextView mEmptyTxt;
    public SwipeRefreshLayout mSwipeRefresh;


    //XUtils图形设置
    public ImageOptions mImageOptions = new ImageOptions.Builder().setAutoRotate(true).setCircular(true)
            .setFadeIn(true).setSize(80,80).setLoadingDrawableId(R.mipmap.ic_launcher).build();
    public String  mUrl = "http://cdn.duitang.com/uploads/blog/201412/07/20141207010951_Ut5zA.thumb.224_0.jpeg";
    private ImageView appImg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_base,container,false);
        x.Ext.init(getActivity().getApplication());
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
        mAppName = (TextView) mView.findViewById(R.id.tool_bar_txt);
        createTask = (TextView) mView.findViewById(R.id.tool_bar_btn);
        mSwipeRefresh = (SwipeRefreshLayout) mView.findViewById(R.id.swipe);
        mEmptyView = mView.findViewById(R.id.empty);
        mLoadingView = mView.findViewById(R.id.loading);
        mEmptyTxt = (TextView) mView.findViewById(R.id.empty_txt);
        appImg = (ImageView) mView.findViewById(R.id.app_img);
        x.image().bind(appImg,mUrl,mImageOptions);

        //recyclerView相关操作
        mRecyclerView.setLayoutManager(onCreateLayoutManager());
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new MyAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mSwipeRefresh.setOnRefreshListener(this);
        //ToolBar创建任务监听事件
        createTask.setOnClickListener(this);
        mEmptyTxt.setOnClickListener(this);

        loadData();
        changeToolBarTxt();
        return mView;
    }

    /**
     * 抽象方法
     */
    public abstract void loadData();
    public abstract MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType);
    public abstract void onBindViewHolder(MyViewHolder holder, int position);
    public abstract void onItemClick(int position, T item, MyViewHolder holder);
    public int getItemCount() {
        return mList.size();
    }
    public int getItemViewType(int position) {
        if (mAdapter.isHasMore()){
            if (position == getItemCount()){
                return  3;
            }
        }
        return 0;
    }

    //ToolBar标题
    public void changeToolBarTxt(){
        mAppName.setText("MyTask2.0");

    }

    /**
     * 布局设置
     * @return
     */
    public RecyclerView.LayoutManager onCreateLayoutManager(){
        return new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        //下拉刷新
        loadData();
    }
    public void onLoadMore(){
        mAdapter.isLoading = true;
        loadData();
    }
    //设置数据加载中、无数据、数据加载完成三种状态切换布局方法
    public void refreshList(){
        if (mList.size() == 0){
            mEmptyView.setVisibility(View.VISIBLE);
            mLoadingView.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.GONE);
        }else {
            mEmptyView.setVisibility(View.GONE);
            mLoadingView.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
        }
        mSwipeRefresh.setRefreshing(false);
    }

    /**
     * 创建任务点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tool_bar_btn:
                startActivity(new Intent().setClass(getContext(), PublishActivity.class));
//                startActivityForResult(new Intent().setClass(getContext(), PublishActivity.class),1);
                break;
            case R.id.empty_txt:
                loadData();
                break;
        }
    }
//
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch (requestCode){
//            case 1:
//
//                break;
//            default:
//                break;
//        }
//    }

    /**
     * Adapter && ViewHolder
     */
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView mName,mTitle,mPrice;
        public ImageView mIcon;

        //针对两个头部写的
        public TextView mPublishCount,mTaskCount;
        public MyViewHolder(View itemView) {
            super(itemView);
            mName = (TextView) itemView.findViewById(R.id.item_name);
            mTitle = (TextView) itemView.findViewById(R.id.item_title);
            mPrice = (TextView) itemView.findViewById(R.id.item_price);
            mIcon = (ImageView) itemView.findViewById(R.id.item_icon);

            mPublishCount = (TextView) itemView.findViewById(R.id.publish_count);
            mTaskCount = (TextView) itemView.findViewById(R.id.task_count);
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        public boolean isLoading;//是否加载下一页
        public boolean hasMore = true;//是否有下一页
        List<T> mList;

        public MyAdapter(List<T> list) {
            mList = list;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 3){
//                return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer_progress_bar,null));
                return  new MyViewHolder(getActivity().getLayoutInflater().inflate(R.layout.item_footer_progress_bar, parent, false));
            }
            return MyBaseFragment.this.onCreateViewHolder(parent,viewType);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            if (holder.getItemViewType() == 3){
                if (isLoading()){

                }else {
                    MyBaseFragment.this.onLoadMore();
                }
                return;
            }

            MyBaseFragment.this.onBindViewHolder(holder,position);

            //点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (position == 0 ){

                    }else {
                        MyBaseFragment.this.onItemClick(position, MyBaseFragment.this.mList.get(position - 1), holder);
                    }
                }
            });

        }

        @Override
        public int getItemViewType(int position) {
            return MyBaseFragment.this.getItemViewType(position);
        }

        @Override
        public int getItemCount() {
            if (hasMore){
                return MyBaseFragment.this.getItemCount()+1;
            }else {
                return MyBaseFragment.this.getItemCount();
            }

        }

        public boolean isLoading() {
            return isLoading;
        }

        public void setIsLoading(boolean isLoading) {
            this.isLoading = isLoading;
        }

        public boolean isHasMore() {
            return hasMore;
        }

        public void setHasMore(boolean hasMore) {
            this.hasMore = hasMore;
        }
    }
}
